package com.llxz.blog.acl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.llxz.blog.acl.entity.Role;
import com.llxz.blog.acl.service.RoleService;
import com.llxz.blog.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author xzs
 * @description: date: 2021/9/4 9:36
 */
@RestController
@RequestMapping("/acl/role")
@CrossOrigin
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping("page/{current}/{limit}")
    public R page(@PathVariable Long current, @PathVariable Long limit, Role role){
        Page<Role> rolePage = new Page<>(current,limit);
        QueryWrapper<Role> wrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(role.getRoleName())){
            wrapper.like("role_name",role.getRoleName());
        }
        roleService.page(rolePage,wrapper);
        return R.ok().data("items",rolePage.getRecords()).data("total",rolePage.getTotal());
    }

    @PostMapping("add")
    public R add(@RequestBody Role role){
        roleService.save(role);
        return R.ok();
    }

    @PutMapping("update")
    public R update(@RequestBody Role role){
        roleService.updateById(role);
        return R.ok();
    }

    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id){
        roleService.removeById(id);
        return R.ok();
    }

}

